## 下载一部小说的前20章
import requests
from lxml import etree
import time

data = 0
main_url = "https://www.bg60.cc/book/3311/1.html"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
    "Referer": "https://www.bg60.cc/book/3311/",
    "Cookie": "Hm_lvt_52624d0257fe48ed9dea61ff01fa3417=1721008461; HMACCOUNT=E845D6D95CED5122; Hm_lpvt_52624d0257fe48ed9dea61ff01fa3417=1721009559"
}

def download_bqg(url):
    global data
    if data <= 20:
        data += 1
    else:
        return
    r = requests.get(url=url, headers=headers)
    if r.status_code == 200:
        tree = etree.HTML(r.text)
    else:
        print("请求异常")

    # 章节的名字
    title = tree.xpath("//h1/text()")[0]
    # 章节的内容
    text = "\n".join(tree.xpath('//div[@id="chaptercontent"]/text()'))
    # 下一章的链接
    print(f"正在下载{title}")
    next_url = tree.xpath('//a[text()="下一章"]/@href')[0]
    with open("全职高手.txt", "a", encoding='utf-8') as f:
        f.write(title + "\n")
        f.write(text + "\n")
    print(f"{title}下载完成")
    time.sleep(3)
    download_bqg("https://www.bg60.cc" + next_url)


download_bqg(main_url)
